<?php
 /**
 * @author Jakub Janský
 */
class Admin_TskupinaController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        $tskupina = new Knihovna_Model_DbTable_Tskupina();
        $this->view->tskupina = $tskupina->FetchAll();
        $flashMessage = $this->_helper->flashMessenger;
        $this->view->message = $flashMessage->getMessages();
    }

    public function addAction()
    {
        $form = new Admin_Form_Tskupina();
        $form->submit->setLabel('Přidej');
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();

            if ($form->isValid($formData)) {
                $nazev = $form->getValue('nazev');

                $tskupina = new Knihovna_Model_DbTable_Tskupina();
                $tskupina->pridejTskupina($nazev);
                $this->_helper->redirector('index');

            } else {
                $form->populate($formData);
            }
        }
    }

    public function editAction()
    {
        $form = new Admin_Form_Tskupina();
        $form->submit->setLabel('Uložit');
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();

            if ($form->isValid($formData)) {

                $id = (int)$form->getValue('id_tskupina');
                $nazev = $form->getValue('nazev');

                $tskupina = new Knihovna_Model_DbTable_Tskupina();
                $tskupina->upravTskupina($id, $nazev);
                $this->_helper->redirector('index');

            } else {
                $form->populate($formData);
            }

        } else {
            $id = $this->_getParam('id_tskupina', 0);

            if ($id > 0) {
                $tskupina = new Knihovna_Model_DbTable_Tskupina();
                $form->populate($tskupina->najdiTskupina($id));
            }
        }
    }

    public function deleteAction()
    {
        $form = new Admin_Form_DeleteTskupina();

        if ($this->getRequest()->isPost() && (false !== $this->getRequest()->getPost('yes', false))) {
            try {
                $id = $this->_getParam('id_tskupina');
                $tskupina = new Knihovna_Model_DbTable_Tskupina();
                $tskupina->smazTskupina($id);
                $this->_helper->flashMessenger('success|Tématická skupina byla úspěšně smazána.');
                $this->_helper->redirector('index');
            } catch (Zend_Db_Exception $error) {
                $this->_helper->flashMessenger('error|' . $error->getMessage());
            }
        } elseif ($this->getRequest()->isPost() && (false !== $this->getRequest()->getPost('no', false))) {
            $this->_redirect('/admin/tskupina/');
        }

        $this->view->form = $form;
    }

}









